﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookManagementWpfApplication.Models;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;


namespace BookManagementWpfApplication.ViewModels
{
    public class ListBookViewModel : INotifyPropertyChanged
    {
        private QSHCEntities _qshcEntities;
        public QSHCEntities QSHCEntities
        {
            get { return _qshcEntities; }
            set
            {
                if (_qshcEntities != value)
                {
                    _qshcEntities = value;
                    OnPropertyChanged("QSHCEntities");
                }
            }
        }

        private ICollectionView _bookICV;
        public ICollectionView BookICV
        {
            get { return _bookICV; }
            set
            {
                if (_bookICV != value)
                {
                    _bookICV = value;
                    OnPropertyChanged("BookICV");
                }
            }
        }

        public ListBookViewModel()
        {
            if (!DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
            {
                QSHCEntities = new Models.QSHCEntities();
                BookICV = CollectionViewSource.GetDefaultView(QSHCEntities.BookDatas.ToList());
 
                BorrowStatusCheck = true;
                ReturnStatusCheck = true;
                DamageStatusCheck = true;
                LostStatusCheck = true;
                RepairStatusCheck = true;
                BuyNewStatusCheck = true;
            }
        }

        public void Filter(string b)
        {
            BookICV.Filter += (o) =>
                {
                    BookData bookData = o as BookData;
                    return (
                        bookData.BookStatus == (BorrowStatusCheck ? (byte)BookStatus.BorrowStatus : 0) ||
                        bookData.BookStatus == (ReturnStatusCheck ? (byte)BookStatus.ReturnStatus : 0) ||
                        bookData.BookStatus == (DamageStatusCheck ? (byte)BookStatus.DamageStatus : 0) ||
                        bookData.BookStatus == (LostStatusCheck ? (byte)BookStatus.LostStatus : 0) ||
                        bookData.BookStatus == (RepairStatusCheck ? (byte)BookStatus.RepairStatus : 0) ||
                        bookData.BookStatus == (BuyNewStatusCheck ? (byte)BookStatus.BuyNewStatus : 0)) && 
                        (string.IsNullOrEmpty(b) ? true : (bookData.BookCode.Contains(b)
                        || bookData.BookName.Contains(b) || bookData.Writer.Contains(b) || bookData.BookType.Contains(b) || bookData.ISBN.Contains(b)
                        ));
                };
        }

        private bool _borrowStatusCheck;
        public bool BorrowStatusCheck
        {
            get { return _borrowStatusCheck; }
            set
            {
                if (_borrowStatusCheck != value)
                {
                    _borrowStatusCheck = value;
                    OnPropertyChanged("BorrowStatusCheck");
                    Filter(FilterText);
                }
            }
        }

        private bool _returnStatusCheck;
        public bool ReturnStatusCheck
        {
            get { return _returnStatusCheck; }
            set
            {
                if (_returnStatusCheck != value)
                {
                    _returnStatusCheck = value;
                    OnPropertyChanged("ReturnStatusCheck");
                    Filter(FilterText);
                }
            }
        }

        private bool _damageStatusCheck;
        public bool DamageStatusCheck
        {
            get { return _damageStatusCheck; }
            set
            {
                if (_damageStatusCheck != value)
                {
                    _damageStatusCheck = value;
                    OnPropertyChanged("DamageStatusCheck");
                    Filter(FilterText);
                }
            }
        }

        private bool _lostStatusCheck;
        public bool LostStatusCheck
        {
            get { return _lostStatusCheck; }
            set
            {
                if (_lostStatusCheck != value)
                {
                    _lostStatusCheck = value;
                    OnPropertyChanged("LostStatusCheck");
                    Filter(FilterText);
                }
            }
        }

        private bool _repairStatusCheck;
        public bool RepairStatusCheck
        {
            get { return _repairStatusCheck; }
            set
            {
                if (_repairStatusCheck != value)
                {
                    _repairStatusCheck = value;
                    OnPropertyChanged("RepairStatusCheck");
                    Filter(FilterText);
                }
            }
        }

        private bool _buyNewStatusCheck;
        public bool BuyNewStatusCheck
        {
            get { return _buyNewStatusCheck; }
            set
            {
                if (_buyNewStatusCheck != value)
                {
                    _buyNewStatusCheck = value;
                    OnPropertyChanged("BuyNewStatusCheck");
                    Filter(FilterText);

                }
            }
        }

        private string _filterText;
        public string FilterText
        {
            get { return _filterText; }
            set
            {
                if (_filterText != value)
                {
                    _filterText = value;
                    OnPropertyChanged("FilterText");
                }
            }
        } 

        private void OnPropertyChanged(string caller)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(caller));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public enum BookStatus
        {
            BorrowStatus = 1,   //ยืม
            ReturnStatus,       //คืน
            DamageStatus,       //ชำรุด
            LostStatus,         //สูญหาย
            RepairStatus,       //ส่งซ่อม
            BuyNewStatus,       //จัดซื้อใหม่
        }
    }
}
